package com.example.xyr.myapplication.ui

import android.content.Intent
import android.net.Uri
import android.os.Handler
import android.os.Message
import android.view.View
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.api.BasicCallback
import com.example.xyr.myapplication.Constant
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.ui.im.ChatUserUtils
import com.example.xyr.myapplication.utils.LogUtil
import com.example.xyr.myapplication.utils.MyIntent
import com.example.xyr.myapplication.utils.ResourceLink
import com.example.xyr.myapplication.utils.StringUtil
import com.example.xyr.myapplication.view.DialogDisplay
import com.example.xyr.myapplication.viewexplosion.ExplosionField
import com.example.xyr.myapplication.viewexplosion.factory.FallingParticleFactory
import kotlinx.android.synthetic.main.activity_login.*


/**
 * Created by ylcf on 2018/8/30.
 */
class LoginActivity : BaseActivity(), View.OnClickListener {

    private lateinit var explosionField: ExplosionField

    private var firstClick = true

    override fun getLayout(): Int = R.layout.activity_login

    override fun init() {
        explosionField = ExplosionField(this, FallingParticleFactory())
        login_bt.background.alpha = 120
        JMessageClient.init(this, true)
    }

    override fun initListener() {
        login_bt.setOnClickListener(this)
    }

    override fun initDate(intent: MyIntent?) {
    }

    override fun onResume() {
        super.onResume()
        //播放小视频
        login_video.setVideoURI(Uri.parse(ResourceLink.getMp4Uri(this)))
        login_video.start()
        login_video.setOnCompletionListener { login_video.start() }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.login_bt -> btLogin()
        }
    }


    private fun btLogin() {
        val msg = login_cleredittext.text.toString().trim()
        if (StringUtil.isNotEmpty(msg)) {
            if (msg == "张迪" || msg == "刘依然") {
                DialogDisplay.getInstance().dialogLoading(this, "登录中")
                jmiRegister(msg)

            } else {
                firstClick = if (this.firstClick) {
                    toastWar(ResourceLink.getError())
                    !firstClick
                } else {
                    toastNo(ResourceLink.getError())
                    !firstClick
                }
            }
        }
    }


    //极光注册
    lateinit var name: String

    private fun jmiRegister(msg: String) {
        name = if (msg == "张迪") "111111" else "222222"
        JMessageClient.register(name, "123456", object : BasicCallback() {
            override fun gotResult(i: Int, s: String) {
                LogUtil.e("tag", "注册： $i  msg = $s")
                if (i == 0) {
                    if (name == "111111") {
                        ChatUserUtils.updateNickName(this@LoginActivity, "老公")
                    } else {
                        ChatUserUtils.updateNickName(this@LoginActivity, "媳妇")
                    }
                }
                jmiLogin(name)
            }
        })
    }

    //极光登录
    private fun jmiLogin(name: String) {
        JMessageClient.login(name, "123456", object : BasicCallback() {
            override fun gotResult(i: Int, s: String) {
                LogUtil.e("tag", "登录： $i  msg = $s")
                if (i == 0) {
                    DialogDisplay.getInstance().dialogCloseLoading(this@LoginActivity)
                    explosionField.addListener(login_bt)
                    onTouch(login_bt)
                    object : Handler() {
                        override fun handleMessage(msg: Message) {
                            super.handleMessage(msg)
                            val intent = Intent(this@LoginActivity, MainActivity::class.java)
                            intent.putExtra(Constant.USER_ID, name)
                            startActivity(intent)
                            finish()
                        }
                    }.sendEmptyMessageDelayed(1, 1000)

                } else {
                    toastWar("网络错误")
                }
            }
        })
    }
}